import { ElMessage } from 'element-plus';
import axios from 'axios';
import { httpReqeustApi } from '/@/api/linxyun/base/index.ts';
/**
 * 排序
 * 通过对象中某个的值继续排序时用
 * mpLevelDataValidArray.sort(compare('showIndex'))
 * @param property
 * @param sortord 'positive' | 'inverted' = 'positive'
 * @returns
 */
export const compare = (property: string, sortord: 'positive' | 'inverted' = 'positive') => {
	return function (a: any, b: any) {
		var value1 = a[property];
		var value2 = b[property];
		// 倒序
		if (sortord === 'inverted') {
			return value2 - value1;
		}
		// 正序
		return value1 - value2;
	};
};

/**
 * 防抖
 * @param fn
 * @param wait
 */
export const debounce = (fn: () => void, wait = 1500) => {
	let timer: NodeJS.Timeout | null = null;
	return () => {
		if (timer) {
			clearTimeout(timer);
		}
		timer = setTimeout(() => {
			fn();
		}, wait);
	};
};

/**
 * 节流
 * @param fn
 * @param wait
 * @returns
 */
export const throttle = (fn: () => void, wait = 1000) => {
	let lastTime: number = 0;
	return () => {
		let nowTime = Date.now();
		if (nowTime - lastTime > wait) {
			fn();
		}
		lastTime = nowTime;
	};
};

export function toHex(data) {
	if (data === undefined) {
		return null;
	}
	let tmp = '';
	if (data.length > 2 && data[0] === '0' && (data[1] === 'x' || data[1] === 'X')) {
		tmp = data.substr(2);
	} else {
		tmp = parseInt(data).toString(16);
	}
	tmp = tmp.toLowerCase();

	while (tmp.length < 4) {
		tmp = '0' + tmp;
	}
	tmp = '0x' + tmp;
	return tmp;
}

export function toHex1(data) {
	if (data === undefined) {
		return null;
	}
	let tmp = '';
	if (data.length > 2 && data[0] === '0' && (data[1] === 'x' || data[1] === 'X')) {
		tmp = data.substr(2);
		tmp = parseInt(tmp, 16);
	}
	return tmp;
}
/**
 * 文件的大小转换成 B KB MB GB
 * @param size
 * @returns
 */
export function sizeTostr(size: number) {
	var data = '';
	if (size < 0.1 * 1024) {
		//如果小于0.1KB转化成B
		data = size.toFixed(2) + 'B';
	} else if (size < 0.1 * 1024 * 1024) {
		//如果小于0.1MB转化成KB
		data = (size / 1024).toFixed(2) + 'KB';
	} else if (size < 0.1 * 1024 * 1024 * 1024) {
		//如果小于0.1GB转化成MB
		data = (size / (1024 * 1024)).toFixed(2) + 'MB';
	} else {
		//其他转化成GB
		data = (size / (1024 * 1024 * 1024)).toFixed(2) + 'GB';
	}
	var sizestr = data + '';
	var len = sizestr.indexOf('.');
	var dec = sizestr.substr(len + 1, 2);
	if (dec == '00') {
		//当小数点后为00时 去掉小数部分
		return sizestr.substring(0, len) + sizestr.substr(len + 3, 2);
	}
	return sizestr;
}

/**
 * 文件下载
 * @param title
 */
export const handleFileDown = async (title: string, url: string) => {
	if (!title) {
		return ElMessage.warning('文件名不存在');
	}
	if (!url) {
		return ElMessage.warning('文件地址不存在');
	}
	let res: any = await axios({ url: url, method: 'POST', data: {}, responseType: 'blob' });
	if (res.data.size > 0) {
		const blob = new Blob([res.data]); //处理文档流
		const fileName = title;
		const down = document.createElement('a');
		down.download = fileName;
		down.style.display = 'none'; //隐藏,没必要展示出来
		down.href = URL.createObjectURL(blob);
		document.body.appendChild(down);
		down.click();
		URL.revokeObjectURL(down.href); // 释放URL 对象
		document.body.removeChild(down); //下载完成移除
	} else {
		ElMessage.warning('文件不存在');
	}
};

/**
 * 匹配中文字符
 * @param str
 * @returns
 */
export function isChinese(str: string) {
	const reg = /[\u4e00-\u9fa5]/;
	return reg.test(str);
}

/**
 * 匹配英文字符
 * @param str
 * @returns
 */
export function isEnglish(str: string) {
	const reg = /[a-zA-Z]/;
	return reg.test(str);
}

export const formatSrc = (src: string) => {
	const setting = localStorage.getItem('setting') ? JSON.parse(localStorage.getItem('setting')!) : {};
	if (src.startsWith('data:')) {
		return src;
	} else if (!src.startsWith('http')) {
		return setting.ImagUrl + src;
	} else {
		return src;
	}
};

/**
 * 获取文本字节长度
 * @param text
 * @returns length
 */
export const getTextByteLength = (text: string) => {
	const enc = new TextEncoder();
	return enc.encode(text).length;
};

/**
 * 获取元素在数组中的下标
 * @param arr
 * @param obj
 * @returns 下标
 */
export const getArrayIndex = (arr: Array<Record<string, any>>, obj: Record<string, any>) => {
	var i = arr.length;
	while (i--) {
		if (arr[i] === obj) {
			return i;
		}
	}
	return -1;
};
